import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * docDimension-单据维度
 * @date: 2019-09-19
 */
import { queryMapIdpValue } from 'services/api';
import { getResponse, createPagination } from 'utils/utils';
import { query as _query, queryDetail as _queryDetail, create as _create, update as _update } from "../services/docDimensionService";
export default {
  namespace: 'docDimension',
  // docDimension 单据维度

  /**
   * state
   */
  state: {
    dimensionList: [],
    // 单据维度列表
    dimensionDetail: {},
    pagination: {},
    // 分页
    valueSourceTypeList: [],
    // 数据源列表
    dimensionTypeList: []
  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, lovCodes, res, dimensionTypeList, valueSourceTypeList;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              lovCodes = payload.lovCodes;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              dimensionTypeList = res.dimensionTypeList, valueSourceTypeList = res.valueSourceTypeList;
              _context.next = 11;
              return put({
                type: 'updateState',
                payload: {
                  valueSourceTypeList: valueSourceTypeList,
                  dimensionTypeList: dimensionTypeList
                }
              });

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 查询数据驱动列表
    query: /*#__PURE__*/_regeneratorRuntime.mark(function query(_ref3, _ref4) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function query$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(_query, payload);

            case 5:
              _context2.t1 = _context2.sent;
              result = (0, _context2.t0)(_context2.t1);
              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  pagination: createPagination(result)
                }
              });

            case 9:
              if (!result) {
                _context2.next = 12;
                break;
              }

              _context2.next = 12;
              return put({
                type: 'updateState',
                payload: {
                  pagination: createPagination(result),
                  dimensionList: result.content
                }
              });

            case 12:
            case "end":
              return _context2.stop();
          }
        }
      }, query);
    }),
    // 通过Id查询数据驱动
    queryDetail: /*#__PURE__*/_regeneratorRuntime.mark(function queryDetail(_ref5, _ref6) {
      var payload, call, put, res, result;
      return _regeneratorRuntime.wrap(function queryDetail$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context3.next = 4;
              return call(_queryDetail, payload);

            case 4:
              res = _context3.sent;
              result = getResponse(res);

              if (!result) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  dimensionDetail: result
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, queryDetail);
    }),
    // 创建数据驱动
    create: /*#__PURE__*/_regeneratorRuntime.mark(function create(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function create$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_create, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, create);
    }),
    // 更新数据驱动
    update: /*#__PURE__*/_regeneratorRuntime.mark(function update(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function update$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_update, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, update);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref11) {
      var payload = _ref11.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};